package com.bianguo.android.functiondemo.view.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.adapter.BaseRecyclerAdapter;
import com.bianguo.android.functiondemo.adapter.MainAdapter;
import com.bianguo.android.functiondemo.proxy.IUser;
import com.bianguo.android.functiondemo.proxy.ProxyFactory;
import com.bianguo.android.functiondemo.proxy.User;
import com.bianguo.android.functiondemo.service.LocalService;
import com.bianguo.android.functiondemo.service.RemoteService;
import com.bianguo.android.functiondemo.utils.MLog;
import com.bianguo.android.functiondemo.utils.PermissionHelper;
import com.bianguo.android.functiondemo.view.widget.SlideItemTouchHelperCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ExecutorService;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements BaseRecyclerAdapter.OnClickItemListener, SlideItemTouchHelperCallback.OnItemTouchListener {

    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;

    private LinearLayoutManager layoutManager;
    private ArrayList<String> list;
    private MainAdapter adapter;
    private ExecutorService executorService;

    @Override
    public int layoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        layoutManager = new LinearLayoutManager(mContext.getApplicationContext());
        layoutManager.setItemPrefetchEnabled(true);
        mRecycler.setLayoutManager(layoutManager);
//        mRecycler.addItemDecoration(new RecycleViewDivider(mContext, LinearLayout.HORIZONTAL));
        list = new ArrayList<>();
        list.add("GSYVideoPlayer");
        list.add("ListView");
        list.add("MediaPlayer");
        list.add("Design");
        list.add("SlideRecyclerView");
        list.add("CustomView");
        list.add("VRShow");
        list.add("EasyAR");
        list.add("Reptile");
        list.add("ListVideo");
        list.add("PhotoView");
        list.add("Touch");
        list.add("VideoCompress");
        list.add("Suspension");
        list.add("RViewHelper");
        adapter = new MainAdapter(mContext, list);
        adapter.setOnClickItemListener(this);
        mRecycler.setAdapter(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SlideItemTouchHelperCallback(this));
        itemTouchHelper.attachToRecyclerView(mRecycler);
//        IUser proxy = (IUser) ProxyFactory.getInstance().getProxy(new User());
        IUser proxy = ProxyFactory.getInstance().getProxy(IUser.class, new User());
        proxy.save();
        MLog.mLog("proxyName = " + proxy.getClass().getName());
        startService(new Intent(this, LocalService.class));
        startService(new Intent(this, RemoteService.class));
    }

    @Override
    public void onClickItemListener(BaseRecyclerAdapter adapter, View view, int position) {
        MLog.mLog("position = " + position);
        String s = list.get(position);
        switch (view.getId()) {
            case R.id.item_main_content_layout:
                if ("GSYVideoPlayer".equals(s)) {
                    startActivity(new Intent(mContext, GSYVideoActivity.class));
                } else if ("ListView".equals(s)) {
                    startActivity(new Intent(mContext, ListViewActivity.class));
                } else if ("MediaPlayer".equals(s)) {
                    boolean permission = PermissionHelper.getInstance(mContext).isPermission(mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PermissionHelper.PERMISSION_WRITER);
                    if (permission) {
                        startActivity(new Intent(mContext, MediaPlayerActivity.class));
                    }
                } else if ("Design".equals(s)) {
                    startActivity(new Intent(mContext, DesignActivity.class));
                } else if ("SlideRecyclerView".equals(s)) {
                    startActivity(new Intent(mContext, SlideActivity.class));
                } else if ("CustomView".equals(s)) {
                    startActivity(new Intent(mContext, CustomActivity.class));
                } else if ("VRShow".equals(s)) {
                    startActivity(new Intent(mContext, VRShowActivity.class));
                } else if ("EasyAR".equals(s)) {
                    startActivity(new Intent(mContext, EasyARActivity.class));
                } else if ("Reptile".equals(s)) {
                    startActivity(new Intent(mContext, ReptileActivity.class));
                } else if ("ListVideo".equals(s)) {
                    startActivity(new Intent(mContext, ListVideoActivity.class));
                } else if ("PhotoView".equals(s)) {
                    ArrayList<String> imageUrls = new ArrayList<>();
                    imageUrls.add("http://img000.hc360.cn/m7/M0F/63/AF/wKhQpFbqIgOELE-rAAAAACFrfd8558.jpg");
                    imageUrls.add("http://pic31.photophoto.cn/20140410/0038038017273919_b.jpg");
                    imageUrls.add("http://pic36.photophoto.cn/20150824/0014026755770049_b.jpg");
                    imageUrls.add("http://pic31.photophoto.cn/20140402/0014026733319281_b.jpg");
                    startActivity(new Intent(mContext, ImageActivity.class)
                            .putStringArrayListExtra("imageUrls", imageUrls));
                } else if ("Touch".equals(s)) {
                    startActivity(new Intent(mContext, TouchActivity.class));
                } else if ("VideoCompress".equals(s)) {
                    startActivity(new Intent(mContext, VideoCompressActivity.class));
                } else if ("Suspension".equals(s)) {
                    startActivity(new Intent(mContext, SuspensionActivity.class));
                } else if ("RViewHelper".equals(s)) {
                    startActivity(new Intent(mContext, RVActivity.class));
                }
                break;
            case R.id.item_main_delete:
                list.remove(position);
                adapter.notifyItemRemoved(position);
                break;
        }
    }

    @Override
    public void onItemSwap(int position, int tagPosition) {
        Collections.swap(list, position, tagPosition);
        adapter.notifyItemMoved(position, tagPosition);
    }

    @Override
    public void removeItem(int position) {
        final int lastPosition = position;
        final String lastMsg = list.get(position);
        list.remove(position);
        adapter.notifyItemRemoved(position);
        Snackbar snackbar = Snackbar.make(mRecycler, "删除成功", Snackbar.LENGTH_LONG);
        snackbar.setAction("撤销", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.add(lastPosition, lastMsg);
                adapter.notifyItemInserted(lastPosition);
            }
        });
        snackbar.setActionTextColor(ActivityCompat.getColor(mContext, R.color.colorMain));
        snackbar.show();
    }
}
